<template>
  <div class="goods-lists">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ name: 'Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form inline size="small" @submit.native.prevent="searchGoodsLists">
      <el-form-item>
        <el-input v-model="search"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleAdd">添加商品</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="showLoading" :data="tableData" style="width: 100%">
      <el-table-column>
        <template slot-scope="{ $index }">{{ $index + 1 }}</template>
      </el-table-column>
      <el-table-column type="index"></el-table-column>
      <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
      <el-table-column prop="goods_price" label="商品价格"> </el-table-column>
      <el-table-column prop="goods_weight" label="商品重量"> </el-table-column>
      <el-table-column label="创建日期">
        <template slot-scope="scope">
          {{ handleDate(scope.row.add_time) }}
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            circle
            @click="editGoods(scope.row.goods_id)"
          ></el-button>

          <el-popconfirm
            title="确定删除吗？"
            @confirm="delGoods(scope.row.goods_id)"
          >
            <el-button
              type="info"
              icon="el-icon-delete"
              size="small"
              slot="reference"
              circle
            ></el-button>
          </el-popconfirm>
          <el-button
            type="success"
            icon="el-icon-check"
            size="small"
            @click="handleRole(scope.row)"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagenum"
      :page-sizes="[5, 10, 20, 30]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
// import { getGoodsLists, delGoods } from "@/http/api";
import * as api from "@/http/api";
import * as dayjs from "dayjs";
export default {
  data() {
    return {
      search: "",
      showLoading: false,
      tableData: [],
      pagenum: 1,
      pagesize: 5,
      total: 0,
      searched: false,
    };
  },
  // filters: {
  //   handleDate(val) {
  //     return dayjs(val).format("YYYY/DD/MM");
  //   },
  // },
  methods: {
    //时间格式化处理
    handleDate(val) {
      console.log(val);
      return dayjs(val).format("YYYY-MM-DD");
    },
    //编辑商品
    editGoods(id) {
      console.log(id);
      this.$router.push({
        name: "GoodsEdit",
        params: {
          id,
        },
      });
    },
    //删除商品
    async delGoods(id) {
      await api.delGoods(id);
      this.handlePagination();
      console.log(id);
    },
    //每页条数变化
    handleSizeChange(val) {
      console.log(val);
      this.pagesize = val;
      this.handlePagination();
    },
    //页码发生变化
    handleCurrentChange(val) {
      this.pagenum = val;
      this.handlePagination();
      console.log(val);
    },
    //获取商品列表
    async getGoodsLists() {
      const res = await api.getGoodsLists({
        pagenum: this.pagenum,
        pagesize: this.pagesize,
      });
      this.total = res.total;
      this.tableData = res.goods;
      console.log(res);
    },
    //搜索商品
    async searchGoodsLists() {
      this.searched = true;
      const res = await api.getGoodsLists({
        pagenum: 1,
        pagesize: 5,
        query: this.search,
      });
      this.total = res.total;
      this.tableData = res.goods;
      console.log(res);
    },
    //分页查询
    async handlePagination() {
      const res = await api.getGoodsLists({
        pagenum: this.pagenum,
        pagesize: this.pagesize,
        query: this.searched ? this.search : "",
      });
      this.total = res.total;
      this.tableData = res.goods;
    },
    //跳转新增商品页面
    handleAdd() {
      this.$router.push({
        name: "GoodsAdd",
      });
    },
  },
  created() {
    this.getGoodsLists();
  },
};
</script>
